package com.tang.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tang.constants.SystemConstants;
import com.tang.dto.talkDto;
import com.tang.enums.AppHttpCodeEnum;
import com.tang.sys.ResponseResult;
import com.tang.sys.entity.Images;
import com.tang.sys.entity.LoginUser;
import com.tang.sys.entity.Talk;
import com.tang.sys.mapper.TalkMapper;
import com.tang.sys.mapper.UserMapper;
import com.tang.sys.service.ITalkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tang.sys.service.IUserService;
import com.tang.sys.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author shankaka
 * @since 2023-05-02
 */
@Service
public class TalkServiceImpl extends ServiceImpl<TalkMapper, Talk> implements ITalkService {

    @Autowired
    private ITalkService iTalkService;

    @Autowired
    private IUserService iUserService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private TalkMapper talkMapper;

    @Override
    public ResponseResult talkList(Integer pageNum, Integer pageSize, Talk talk) {
        // 查询没有删除和状态正常的
        LambdaQueryWrapper<Talk> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Talk::getStatus, SystemConstants.TALKSTATUSOK);
        wrapper.eq(Talk::getDelFlag, SystemConstants.TALKSTATUSOK);
        wrapper.orderByDesc(Talk::getCreateTime);

        Page page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        page(page, wrapper);

        PageVo pageVo = new PageVo(page.getRecords(), page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult getAllList(Integer pageNum, Integer pageSize, talkDto talkdto) {
        LambdaQueryWrapper<Talk> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(talkdto.getTalkTitle()),Talk::getTalkTitle,talkdto.getTalkTitle());

        Page<Talk> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);

        page(page,wrapper);

        PageVo pageVo = new PageVo(page.getRecords(),page.getTotal());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTalk(Talk talk) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //获取userid
        Integer id = Math.toIntExact(loginUser.getUser().getId());

        Talk talk1 = new Talk();
        Date date = new Date();

        talk1.setTalkTitle(talk.getTalkTitle());
        talk1.setTalkImage(talk.getTalkImage());
        talk1.setStatus(talk.getStatus());
        talk1.setDelFlag(0);
        talk1.setIsTop(1);
        talk1.setCreateTime(date);
        talk1.setId(id);
        talkMapper.insert(talk1);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteTalk(Long id) {
        LambdaQueryWrapper<Talk> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Talk::getId,id);
        this.baseMapper.delete(wrapper);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getTalkinfo(Long id) {
        Talk byId = iTalkService.getById(id);
        return ResponseResult.okResult(byId);
    }

    @Override
    public ResponseResult updateTalk(Talk talks) {
        Talk byId = iTalkService.getById(talks);
        byId.setTalkTitle(talks.getTalkTitle());
        byId.setTalkImage(talks.getTalkImage());
        byId.setCreatePe(talks.getCreatePe());
        // 判断传入的 status 值是否为 null
        if (talks.getStatus() != null) {
            int status = talks.getStatus();
            if (status == 0 || status == 1) {
                byId.setStatus(status);
                this.baseMapper.updateById(byId);
                return ResponseResult.okResult();
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.TALK_STATUS);
            }
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.INVALID_PARAM);
        }

    }

}
